from pyglet.gl import *
import pyglet
from pyglet.window import *
import os, random
from pyglet import image
"""
When you draw on a window in pyglet, you are drawing to an OpenGL context. Every window has its own context, which is created when the window is created. You can access the window's context via its context attribute.
"""

window = pyglet.window.Window(width=640, height=480, resizable=True,
			style=Window.WINDOW_STYLE_DIALOG)

num=50

twinkle = True	# Twinkling stars
stars = []	# array of our stars
zoom = -15.0	# viewing distance away from stars
tilt = 90.0		# tilt the view
spin = 0.0		# spin twinkling stars


class Star:
  	def __init__(self, index):
		self.index = index
		# stars current angle
		self.angle = 0.0

		#color
		self.r=random.randrange(0, 256)
		self.g=random.randrange(0, 256)
		self.b=random.randrange(0, 256)

		#dist from center
		self.dist = (float(index)/num) * 5.0

	def draw(self):
	  	global spin, tilte, twinkle, stars,num
	  	glLoadIdentity()	# reset before we draw a star
		glTranslatef(0.0, 0.0, zoom)	# zoom into the screen
		glRotatef(tilt, 1.0, 0.0, 0.0)	# tilt the view 
		glRotatef(self.angle, 0.0, 1.0, 0.0)	# rotate to the current stars angle
		glTranslatef(self.dist, 0.0, 0.0)	# move forward on x plane

		glRotatef(-self.angle, 0.0, 1.0, 0.0)	# cancel the current stars angle
		glRotatef(-tilt, 1.0, 0.0, 0.0)	# cancel the screen tilt

		if twinkle:
		  	glColor4ub( stars[num - self.index -1].r,
						stars[num - self.index -1].g,
						stars[num - self.index -1].b, 255)

			glBegin(GL_QUADS)
			glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 0.0)
			glTexCoord2f(1.0, 0.0);	glVertex3f(1.0, -1.0, 0.0)
			glTexCoord2f(1.0, 1.0);	glVertex3f(1.0, 1.0, 0.0)
			glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0)
			glEnd()

		#rotate the star on the z axis
		glRotatef(spin, 0.0, 0.0, 1.0)
		# assign a color using bytes
		glColor4ub( self.r, self.g, self.b, 255)

		# draw a texture quad
		glBegin(GL_QUADS)
		glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 0.0)
		glTexCoord2f(1.0, 0.0);	glVertex3f(1.0, -1.0, 0.0)
		glTexCoord2f(1.0, 1.0);	glVertex3f(1.0, 1.0, 0.0)
		glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0)
		glEnd()

		spin+=0.01
		self.angle += 1.0*self.index/50	# change the angle of the star
		self.dist -= 0.01	# change the distance of the star

		# is the star in the middle yet?
		if self.dist < 0.0:
		  	self.dist += 5.0
			self.r = random.randrange(0, 256)
			self.g = random.randrange(0, 256)
			self.b = random.randrange(0, 256)

@window.event
def update(dt):
  	pass
pyglet.clock.schedule(update)

@window.event
def on_resize(width, height):
	if height==0:
		height = 1
	glViewport(0, 0, width, height)

	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()

	# Calculate the aspect ratio of the window
	gluPerspective(45.0, 1.0*width/height, 0.1, 100.0)

	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	return pyglet.event.EVENT_HANDLED

def load_gl_textures():
  	global texture
	t_file = os.path.join('data', 'star.bmp')
  	texture_image = image.load(t_file)
	texture = texture_image.get_texture()
	glBindTexture(GL_TEXTURE_2D, texture.id)

	glBindTexture(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
	glBindTexture(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

	
def init():
  	"""
	Pyglet oftentimes calls this setup()	
	"""
	glEnable(GL_TEXTURE_2D)	# enable texture mapping

	load_gl_textures()

  	glShadeModel(GL_SMOOTH)	# Enables smooth shading
	glClearColor(0.0, 0.0, 0.0, 0.0) #Black background
	glClearDepth(1.0)		# Depth buffer setup
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)	# Really nice perspective calculations
	glBlendFunc(GL_SRC_ALPHA, GL_ONE)	# set the blending function for translucency
	glEnable(GL_BLEND)		# enable blending

	for i in range(50):
	  	stars.append(Star(i))


@window.event
def on_draw():
  	# Here we do all the drawing
	glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT)
	glBindTexture(GL_TEXTURE_2D, texture.id)	# select our texture

	# loop through all the stars
	for star in stars:
	  star.draw()
  	
@window.event
def on_key_press(symbol, modifiers):
  global twinkle, tilt, zoom

  if symbol == key.RETURN:
		exit()
  elif symbol == key.T:
		twinkle = not twinkle
  elif symbol == key.UP:
	tilt -=0.5
  elif symbol == key.DOWN:
	tilt +=0.5
  elif symbol == key.PAGEUP:
	zoom-=0.4
  elif symbol == key.PAGEDOWN:
	zoom+=0.4
	

init()
	

pyglet.app.run()
